// Copyright 2015 Palantir Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0.

@use "sass:list";
@import "../../common/variables";
@import "../forms/common";
@import "./common";

.#{$ns}-button-group {
  display: inline-flex;

  .#{$ns}-button {
    // ensure button can never be smaller than its default size
    flex: 0 0 auto;
    position: relative;
    z-index: list.index($control-group-stack, "button-default");

    // the ordering of these z-index CSS rules is particular because of CSS
    // selector specificity. specifically, disabled styles should have
    // precedence over hover and active styles to prevent mouse interactions on
    // disabled buttons from reordering elements in the stack.

    &:focus {
      z-index: list.index($control-group-stack, "button-focus");
    }

    &:hover {
      z-index: list.index($control-group-stack, "button-hover");
    }

    &:active,
    &.#{$ns}-active {
      z-index: list.index($control-group-stack, "button-active");
    }

    &:disabled,
    &.#{$ns}-disabled {
      z-index: list.index($control-group-stack, "button-disabled");
    }

    &[class*="#{$ns}-intent-"] {
      z-index: list.index($control-group-stack, "intent-button-default");

      &:focus {
        z-index: list.index($control-group-stack, "intent-button-focus");
      }

      &:hover {
        z-index: list.index($control-group-stack, "intent-button-hover");
      }

      &:active,
      &.#{$ns}-active {
        z-index: list.index($control-group-stack, "intent-button-active");
      }

      &:disabled,
      &.#{$ns}-disabled {
        z-index: list.index($control-group-stack, "intent-button-disabled");
      }
    }
  }

  // support wrapping buttons in a tooltip, which adds a wrapper element
  &:not(.#{$ns}-minimal),
  &.#{$ns}-outlined {
    > .#{$ns}-popover-wrapper:not(:first-child) .#{$ns}-button,
    > .#{$ns}-button:not(:first-child) {
      border-bottom-left-radius: 0;
      border-top-left-radius: 0;
    }

    > .#{$ns}-popover-wrapper:not(:last-child) .#{$ns}-button,
    > .#{$ns}-button:not(:last-child) {
      border-bottom-right-radius: 0;
      border-top-right-radius: 0;
    }
  }

  &:not(.#{$ns}-minimal):not(.#{$ns}-outlined) {
    > .#{$ns}-popover-wrapper:not(:last-child) .#{$ns}-button,
    > .#{$ns}-button:not(:last-child) {
      margin-right: -$button-border-width;
    }
  }

  &.#{$ns}-minimal,
  &.#{$ns}-outlined {
    .#{$ns}-button {
      @include pt-button-minimal;
    }

    @media (forced-colors: active) and (prefers-color-scheme: dark) {
      // Because we give even minimal buttons a border in high contrast mode, we need to handle border styles
      // similar to how we handle non-minimal buttons

      &:not(:first-child) {
        border-bottom-left-radius: 0;
        border-left: none;
        border-top-left-radius: 0;
      }

      &:not(:last-child) {
        border-bottom-right-radius: 0;
        border-top-right-radius: 0;
        margin-right: -$button-border-width;
      }
    }
  }

  &.#{$ns}-outlined {
    > .#{$ns}-button {
      @include pt-button-outlined;
    }

    &:not(.#{$ns}-vertical) {
      > .#{$ns}-button:not(:last-child) {
        border-right: none;
      }
    }
  }

  .#{$ns}-popover-wrapper,
  .#{$ns}-popover-target {
    display: flex;
    flex: 1 1 auto;
  }

  &.#{$ns}-fill {
    display: flex;
    width: 100%;
  }

  .#{$ns}-button.#{$ns}-fill,
  &.#{$ns}-fill .#{$ns}-button:not(.#{$ns}-fixed) {
    flex: 1 1 auto;
  }

  &.#{$ns}-vertical {
    align-items: stretch;
    flex-direction: column;
    vertical-align: top;

    &.#{$ns}-fill {
      height: 100%;
      width: unset;
    }

    .#{$ns}-button {
      // we never want that margin-right when vertical
      margin-right: 0 !important; /* stylelint-disable-line declaration-no-important */
      // needed to ensure buttons take up the full width when wrapped in a Popover:
      width: 100%;
    }

    &:not(.#{$ns}-minimal),
    &.#{$ns}-outlined {
      > .#{$ns}-popover-wrapper:first-child .#{$ns}-button,
      > .#{$ns}-button:first-child {
        border-radius: $pt-border-radius $pt-border-radius 0 0;
      }

      > .#{$ns}-popover-wrapper:last-child .#{$ns}-button,
      > .#{$ns}-button:last-child {
        border-radius: 0 0 $pt-border-radius $pt-border-radius;
      }
    }

    &:not(.#{$ns}-minimal):not(.#{$ns}-outlined) {
      > .#{$ns}-popover-wrapper:not(:last-child) .#{$ns}-button,
      > .#{$ns}-button:not(:last-child) {
        margin-bottom: -$button-border-width;
      }
    }

    &.#{$ns}-outlined {
      > .#{$ns}-button:not(:last-child) {
        border-bottom: none;
      }
    }
  }

  &.#{$ns}-align-left .#{$ns}-button {
    text-align: left;
  }
}
